package com.ueboot.weishop.admin.controller.customer;

import com.alibaba.fastjson.JSON;
import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.admin.controller.customer.vo.CustomerCouponFindReq;
import com.ueboot.weishop.admin.controller.customer.vo.CustomerCouponReq;
import com.ueboot.weishop.admin.controller.customer.vo.CustomerCouponResp;
import com.ueboot.weishop.entity.customer.CustomerCouponEntity;
import com.ueboot.weishop.service.customer.CustomerCouponService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Neel on 2017/9/18.
 */
@Slf4j
@RestController
@RequestMapping(value = "/customers/coupons")
public class CustomerCouponController {

    @Autowired
    private CustomerCouponService customerCouponService;

    @RequiresPermissions("customers.coupons:read")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<CustomerCouponResp>> page(@PageableDefault Pageable pageable, @RequestBody(required = false) CustomerCouponFindReq req) {
        log.info("/customers/coupons/page/");

        Page<CustomerCouponEntity> entities = customerCouponService.findBy(pageable);

        Page<CustomerCouponResp> body = entities.map(entity -> {
            CustomerCouponResp resp = new CustomerCouponResp();
            BeanUtils.copyProperties(entity, resp);
            resp.setEnabled(entity.getEnabled() ? "是" : "否");
            return resp;
        });

        log.info("response: {}", JSON.toJSON(body));
        return new Response<>(body);
    }


    @RequiresPermissions("customers.coupons:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody CustomerCouponReq req) {
        log.info("/customers/coupons/save/");
        CustomerCouponEntity entity = null;
        if (req.getId() == null) {
            entity = new CustomerCouponEntity();
        } else {
            entity = customerCouponService.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);

        entity.setEnabled("是".equals(req.getEnabled()));

        customerCouponService.save(entity);

        return new Response<>();
    }


    @RequiresPermissions("customers.coupons:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] id) {
        log.info("/customers/coupons/delete/{}", id);
        customerCouponService.delete(id);
        return new Response<>();
    }

    @RequiresPermissions("customers.coupons:delete")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Response<Void> remove(Long id) {
        log.info("/customers/coupons/remove/{}", id);
        customerCouponService.delete(id);
        return new Response<>();
    }


    @RequiresPermissions("customers.coupons:read")
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public Response<Void> find(@RequestBody CustomerCouponFindReq req) {
        log.info("/customers/coupons/find/");

        return new Response<>();
    }


}
